package testing;

import com.google.gdata.client.appsforyourdomain.AppsGroupsService;
import com.google.gdata.client.appsforyourdomain.EmailListRecipientService;
import com.google.gdata.client.appsforyourdomain.EmailListService;
import com.google.gdata.client.appsforyourdomain.NicknameService;
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.data.Link;
import com.google.gdata.data.appsforyourdomain.AppsForYourDomainException;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class AppsForYourDomainClientAPI extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
		Logger LOGGER = Logger.getLogger(AppsForYourDomainClientAPI.class
				.getName());
		String APPS_FEEDS_URL_BASE = "https://apps-apis.google.com/a/feeds/";
		String SERVICE_VERSION = "2.0";
		String domainUrlBase;
		EmailListRecipientService emailListRecipientService;
		EmailListService emailListService;
		NicknameService nicknameService;
		UserService userService;
		AppsGroupsService groupService;

		String domain = "celestica.com";
		String adminEmail = "appengine01-cls@celestica.com";
		String adminPassword = "good4clstesting";

		// Configure all of the different Provisioning services
		userService = new UserService(
				"gdata-sample-AppsForYourDomain-UserService");
		try {
			userService.setUserCredentials(adminEmail, adminPassword);
		} catch (AuthenticationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		nicknameService = new NicknameService(
				"gdata-sample-AppsForYourDomain-NicknameService");
		try {
			nicknameService.setUserCredentials(adminEmail, adminPassword);
		} catch (AuthenticationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		emailListService = new EmailListService(
				"gdata-sample-AppsForYourDomain-EmailListService");
		try {
			emailListService.setUserCredentials(adminEmail, adminPassword);
		} catch (AuthenticationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		emailListRecipientService = new EmailListRecipientService(
				"gdata-sample-AppsForYourDomain-EmailListRecipientService");
		try {
			emailListRecipientService.setUserCredentials(adminEmail,
					adminPassword);
		} catch (AuthenticationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			groupService = new AppsGroupsService(adminEmail, adminPassword,
					domain, "gdata-sample-AppsForYourDomain-AppsGroupService");
		} catch (AuthenticationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		domainUrlBase = APPS_FEEDS_URL_BASE + domain + "/";

		LOGGER.log(Level.INFO, "Retrieving all users.");

		URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/");
		UserFeed currentPage = null;
		Link nextLink;
		int i = 0;
		
		out.println(" <table id='draft' class='display'> ");
		out.println(" <thead> ");
		out.println(" <tr> ");
		out.println(" <td>  E-mail </td> ");
		out.println(" <td> Fullname </td> ");
		out.println(" </tr> ");
		out.println("  </thead> ");
		out.println("  <tbody> ");
		
		

			   try {
				currentPage = userService.getFeed(retrieveUrl, UserFeed.class);
			} catch (AppsForYourDomainException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		do {

			// allUsers.getEntries().addAll(currentPage.getEntries());
			nextLink = currentPage.getLink(Link.Rel.NEXT, Link.Type.ATOM);
			UserEntry entry = currentPage.getEntries().get(i);
			String userName = entry.getTitle().getPlainText().toLowerCase()
					.trim();
			String fullName = entry.getName().getGivenName().toLowerCase()
					.trim()
					+ " "
					+ entry.getName().getFamilyName().toLowerCase().trim();
			String eMail = userName + "@" + domain;

			out.println("<tr>");
			out.println("<td >" + eMail + "</td>");
			out.println("<td >" + fullName + "</td>");
			out.println("</tr>");
			            
			if (nextLink != null) {
				retrieveUrl = new URL(nextLink.getHref());
			}
			if (i==50){
				/*
				try {
					currentPage = userService.getFeed(retrieveUrl, UserFeed.class);
				} catch (AppsForYourDomainException e) {
					e.printStackTrace();
				} catch (ServiceException e) {
					e.printStackTrace();
				}*/
				out.println("</tbody>");
				out.println(" </table> ");
				break;
			}
			i++;
		} while (nextLink != null);
		
		//out.println("</tbody>");
		//out.println(" </table> ");
	}
}
